<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\learn\Book;

use app\common\model\learn\book\Title as BookModel;

/**
 * 题目模型
 * Class Category
 * @package app\store\model
 */
class Book extends BookModel {

	public function getList(array $param = []): \think\Paginator {
		// 查询参数
		$params = $this->setQueryDefaultValue($param, [
			'title' => '', // 文章标题
			'book' => '', // 文章分类id
			'subjectNo' => '', // 文章分类id
			'type' => -1, // 文章分类id
			'status' => -1, // 文章状态
		]);
		// 检索查询条件
		$filter = [];
		// 文章标题
		!empty($params['title']) && $filter[] = ['title', 'like', "%{$params['title']}%"];
		// 文章分类id
		!empty($params['book']) && $filter[] = ['book_name|book_name2', '=', $params['book']];
		$params['type'] != -1 && $filter[] = ['type', '=', $params['type']];
		// 文章状态
		$params['status'] > -1 && $filter[] = ['status', '=', $params['status']];
		// 查询列表数据
		return $this
			->where($filter)
			->where('is_delete', '=', 0)
			->order(['sort' => 'asc', 'create_time' => 'desc'])
			->paginate(15);
	}

	/**批量导入
		             * @param array $data
		             * @return mixed
	*/
	public function batchImport(array $data) {

		foreach ($data as $key => $row) {
			$row['store_id'] = self::$storeId;
			$insertData[] = $row;

		}

		// 附表
		$this->saveAll($insertData);

		return true;

	}


	/**
	 * 添加新记录
	 * @param $data
	 * @return bool
	 */
	public function add($data): bool {

		$data['store_id'] = self::$storeId;
		return $this->save($data);
	}

	/**
	 * 编辑记录
	 * @param $data
	 * @return bool
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function edit($data): bool {

		return $this->save($data) !== false;
	}

	public function setDelete(array $goodsIds): bool {

		// 批量更新记录
		return static::updateBase(['is_delete' => 1], [['subject_id', 'in', $goodsIds]]);
	}
}
